﻿using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Web.Mvc;
using BuildServer.Models;

namespace BuildServer.Controllers
{
    [Authorize]
    public class DefinitionController : Controller
    {
        private BuildServerDbContext db = new BuildServerDbContext();

        //
        // GET: /Definition/

        public ActionResult Index()
        {
            db.Database.Log = (msg) => System.Diagnostics.Trace.WriteLine(msg);
            var builddefinitions = db.BuildDefinitions.Include(b => b.BuildTemplate);
            return View(builddefinitions);
        }

        //
        // GET: /Definition/Details/5

        public ActionResult Details(int id = 0)
        {
            BuildDefinition builddefinition = db.BuildDefinitions.Find(id);
            if (builddefinition == null)
            {
                return HttpNotFound();
            }
            return View(builddefinition);
        }

        //
        // GET: /Definition/Create

        public ActionResult Create()
        {
            AddBuildTemplates(null);
            return View();
        }

        //
        // POST: /Definition/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(BuildDefinition builddefinition)
        {
            if (ModelState.IsValid)
            {
                db.BuildDefinitions.Add(builddefinition);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            AddBuildTemplates(builddefinition);
            return View(builddefinition);
        }

        //
        // GET: /Definition/Edit/5

        public ActionResult Edit(int id = 0)
        {
            BuildDefinition builddefinition = db.BuildDefinitions.Find(id);
            if (builddefinition == null)
            {
                return HttpNotFound();
            }
            AddBuildTemplates(builddefinition);
            return View(builddefinition);
        }


        //
        // POST: /Definition/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(BuildDefinition builddefinition)
        {
            if (ModelState.IsValid)
            {
                db.Entry(builddefinition).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            AddBuildTemplates(builddefinition);
            return View(builddefinition);
        }


        //
        // GET: /Definition/Copy/5

        public ActionResult Copy(int id = 0)
        {
            BuildDefinition builddefinition = db.BuildDefinitions.Find(id);
            if (builddefinition == null)
            {
                return HttpNotFound();
            }
            builddefinition.Name += " (Copy)";
            AddBuildTemplates(builddefinition);
            return View(builddefinition);
        }

        //
        // POST: /Definition/Copy/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Copy(int copy_id, BuildDefinition builddefinition)
        {
            if (ModelState.IsValid)
            {
                db.Database.Log = (msg) => System.Diagnostics.Trace.WriteLine(msg);

                // add new build definition
                db.BuildDefinitions.Add(builddefinition);

                // copy properties
                var properties = db.BuildProperties.Where(x => x.BuildDefinition.Id == copy_id);
                foreach (var prop in properties)
                {
                    var newProp = db.BuildProperties.Create();
                    newProp.Name = prop.Name;
                    newProp.Value = prop.Value;
                    newProp.BuildDefinition = builddefinition;
                    db.BuildProperties.Add(newProp);
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            AddBuildTemplates(builddefinition);
            return View(builddefinition);
        }

        //
        // GET: /Definition/Delete/5

        public ActionResult Delete(int id = 0)
        {
            BuildDefinition builddefinition = db.BuildDefinitions.Find(id);
            if (builddefinition == null)
            {
                return HttpNotFound();
            }
            return View(builddefinition);
        }

        //
        // POST: /Definition/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            BuildDefinition builddefinition = db.BuildDefinitions.Find(id);
            db.BuildDefinitions.Remove(builddefinition);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private void AddBuildTemplates(BuildDefinition model)
        {
            // required for @Html.DropDownList("BuildTemplateId")
            if (model == null)
            {
                ViewBag.BuildTemplateId = new SelectList(db.BuildTemplates, "Id", "Name");
            }
            else
            {
                ViewBag.BuildTemplateId = new SelectList(db.BuildTemplates, "Id", "Name", model.BuildTemplateId);
            }

        }
    }
}